$(function () {
  var verifyBtnInterval = null;
  var verifyBtnTime = 60;
  var sendSuccessTime = 0;
  getCaptcha();

  $(document).on('input', '#username', _.debounce(handleUsernameBlur, 500));
  $(document).on('click', '#verify-btn', handleVerifyBtnClick);
  $(document).on('input', '#password', _.debounce(handlePasswordIpt, 500));
  $(document).on('input', '#check-password', _.debounce(handlePasswordCheckIpt, 500));
  $(document).on('click', '#submit', handleSubmit);
  $(document).on('click', '#captcha-box', getCaptcha);
  $(document).on('click', '#serviceRule', showServiceRule);
  $(document).on('click', '#serviceRuleModalBtn', hideServiceRule);

  function handleUsernameBlur(e) {
    var phone = $('#username').val();
    if (!phone) {
      $('.login-ntf.name').text('手机号不能为空');
      $('#verify-btn').attr('disabled', true);
      return;
    }
    if (isNaN(+phone)) {
      $('.login-ntf.name').text('手机号码格式不正确');
      $('#verify-btn').attr('disabled', true);
      return;
    }
    
    $.get('/api/checkPhone?phone=' + phone).then(function (res) {
      if (_.get(res, 'code') !== 0) {
        $('.login-ntf.name').text((_.get(res, 'msg', '手机号码不正确')));
        $('#verify-btn').attr('disabled', true);
      } else {
        if(sendSuccessTime>0){
          getCaptcha();
          $('#word-captcha').val('')
          sendSuccessTime--;
        }
        $('.login-ntf.name').text('');
        $('#verify-btn').attr('disabled', false);
      }
    }).catch(function(res){
      $('.login-ntf.name').text((_.get(res.responseJSON, 'msg', '手机号码不正确')));
      $('#verify-btn').attr('disabled', true);
    });
  }

  function handlePasswordIpt(e) {
    var psw = $('#password').val();
    if (psw.length < 6) {
      $('.login-ntf.password').text('密码不能少于6位');
    } else {
      $('.login-ntf.password').text('');
    }
  }

  function handlePasswordCheckIpt(e) {
    var psw = $('#password').val();
    var pswCheck = $('#check-password').val();
    if (psw !== pswCheck) {
      $('.login-ntf.password-check').text('两次输入的密码不相同');
    } else {
      $('.login-ntf.password-check').text('');
    }
  }

  function handleVerifyBtnClick() {
    var phone = $('#username').val();
    $('#verify-btn').attr('disabled', true);
    $.get('/api/phoneVerifyCode?phone=' + phone + '&cap=' + $('#word-captcha').val()).then(function (res) {
      $('#verify-btn').attr('disabled', false);
      if (_.get(res, 'code') === 0) {
        sendSuccessTime++;
        $('#username').attr('disabled', true);
        $('#verify-btn').attr('disabled', true);
        verifyBtnInterval = setInterval(function () {
          verifyBtnTime--;
          if (verifyBtnTime === 0) {
            clearInterval(verifyBtnInterval);
            verifyBtnTime = 60;
            $('#verify-btn').attr('disabled', false).text('获取验证码');
            $('#username').attr('disabled', false);
          } else {
            $('#verify-btn').text(verifyBtnTime + ' S');
          }
        }, 1000);
      }
    }).catch(function (res) {
      $('#verify-btn').attr('disabled', false);
      getCaptcha();
    });
  }

  function handleSubmit(e) {
    var userphone = $('#username').val();
    var password = $('#password').val();
    var captcha = $('#captcha').val();
    var readInfo = $('#read-info').is(':checked');
    var passwordCheck = $('#check-password').val();
    if (!userphone ||
      (!password) ||
      (!captcha) ||
      (!passwordCheck) ||
      $('.login-ntf.name').text() ||
      $('.login-ntf.password').text() ||
      $('.login-ntf.password-check').text()
    ) {
      warning_prompt('信息缺失');
      return;
    }
    if (password !== passwordCheck) {
      warning_prompt('两次密码输入不一致');
      return;
    }
    if (!readInfo) {
      warning_prompt('请先阅读《浙江在线预约服务协议》');
      return;
    }
    $('#submit').attr('disabled', true);
    $.post('/api/register', {
      userphone: userphone,
      password: Crypto.encryption(password),
      captcha: captcha,
    }).then(function(res) {
      $('#submit').attr('disabled', false);
      if(res.code === 0) {
        success_prompt('注册成功', 1500);
        setTimeout(function() {
          window.location = '/login';
        }, 1000)
      }
    }).catch(function(res){
      $('#submit').attr('disabled', false);
    });
  }

  function getCaptcha() {
    $.get('/api/registerCaptcha').then(function(res) {
      if (res.code === 0) {
        $('#captcha-box').html(res.data);
      }
    });
  }

  function showServiceRule() {
    $('#serviceRuleModal').modal('show');
  }
  
  function hideServiceRule() {
    $('#serviceRuleModal').modal('hide');
  }
});